package cheng.rui.hong.logic;

import java.text.NumberFormat;

import cheng.rui.hong.Calculator;
import cheng.rui.hong.frame.Frame;

public class Controller {

	private Calculator calc=new Calculator();
	private NumberFormat numFormat=NumberFormat.getInstance();
	
	private Frame frame;
	private MementoOperation operation;
	public Controller(Frame frame){
		this.frame=frame;
		operation=new MementoOperation(frame);
	}
	
	public boolean appNum(String numText){
		boolean success=false;
		if(frame.getShowText() !=null && frame.getShowText().length()>0){
			
			if(!operation.getCurrentLine().equals("") && this.checkPrevinput()==1){
				operation.setCurrentLine("");
				operation.appShowText("\n"+numText);
			operation.appCurrentLine(numText);
			}else{
				operation.appShowText(numText);
				operation.appCurrentLine(numText);	
			}
			
		}else{
			operation.appShowText(numText);
			operation.appCurrentLine(numText);
		}
		return true;
	}
	
	public int checkPrevinput(){
		int i=0;
		try{
			new Double(operation.getPrevInput());
		}catch(Exception e){
			i=1;
		}
		
		return i;
	}
	
	public boolean appText(String text){
		boolean success=false;
		if(frame.getShowText() !=null && frame.getShowText().length()>0){
			
			if(operation.getCurrentCalcEnum() != CalcEnum.DEFAULT){
				try{
					double d=new Double(operation.getCurrentLine());
					this.equalsPressed();
				}catch(Exception e){
					System.err.println("appText(): changed calc type");
				}
				
			}
			
			if(!operation.getCurrentLine().equals("")){
				try{
					double curNum=new Double(operation.getCurrentLine());
					operation.setPrevNum(curNum);
				    }catch(Exception e){
				    	System.err.println("appText(): not a number ");

				    }
				operation.setCurrentLine("");
				operation.appShowText("\n"+text);
					operation.appCurrentLine(text);
			}else{
				operation.appShowText(text);
				operation.appCurrentLine(text);
			}
		}
		return success;
	}
	
	public void pressNum(String i){
		appNum(i);
		
		operation.setPrevInput(i);
	}
	
	public void dotPressed(){
		//this.showArea.append(".");
		this.appNum(".");
		operation.setPrevInput("0");
	}
	
	public void cancelPressed(){
//		this.showArea.setText("");
//		this.currentCalcEnum=CalcEnum.DEFAULT;
//		this.currentLine="";
//		this.prevNum=0.0;
//		this.prevInput="";
		this.operation=operation.cancel();
	}
	
//	private void continuousCalc(){
//		
//	}
	
	public void equalsPressed(){
		if(operation.getCurrentCalcEnum() == CalcEnum.DEFAULT){
			return;
		}
		try{
			double curNum=new Double(operation.getCurrentLine());
			double result=0.0;
			calc.setA(operation.getPrevNum());
			calc.setB(curNum);
			switch(operation.getCurrentCalcEnum()){
			case ADD: result=calc.add();break;
			case MINUS: result=calc.minus();break;
			case MULTIPLY: result=calc.multiply();break;
			case DIVIDE: try{result=calc.divide();}catch(Exception e){ return;}break;
			}
			operation.appShowText("\n=\n");
			operation.setCurrentLine("");
			
			this.appNum(numFormat.format(result));
			operation.setPrevNum(result);
			operation.setCurrentCalcEnum(CalcEnum.DEFAULT);
			//this.currentLine="";
		}catch(Exception e){
			System.err.println("equalsPressed(): not a number ");
		}
		
		operation.setPrevInput("=");
		
	}
	public void calculatePressed(CalcEnum calcType){
		
		if(calcType != CalcEnum.DEFAULT){
			//this.showArea.append("\n");
			switch(calcType){
			case ADD: this.appText("+");break;
			case MINUS: this.appText("-");break;
			case MULTIPLY: this.appText("*");break;
			case DIVIDE: this.appText("/");break;
			}
			operation.setCurrentCalcEnum(calcType);
		}
		
		operation.setPrevInput("calc");
	}
	
	
}
